package com.hyy.taobao.controller;

import com.hyy.taobao.api.IUserService;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/deleteUser")
public class DeleteUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;UTF-8");
        HttpSession session = req.getSession();
        User myUser = (User) session.getAttribute("myUser");
        req.setAttribute("question", myUser.getSecurityQuestion());
        String userName = myUser.getUserName();
        String userPassword = req.getParameter("userPassword");
        String securityAnswer = req.getParameter("userSecurityAnswer");
        User user = new User(userName, userPassword, securityAnswer);
        IUserService iUserService = new UserServiceImpl();
        boolean judgement = iUserService.deleteUser(user);
        if (judgement) {
            req.setAttribute("msg", "注销成功！");
            req.getRequestDispatcher("result.jsp").forward(req, resp);
        } else {
            req.setAttribute("msg", "注销失败，用户名、密码或密保可能有错误的！");
            req.getRequestDispatcher("deleteUserWeb.jsp").forward(req, resp);
        }
    }
}